﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;

namespace NotificationsApp
{
    static class Program
    {
        public static TeamWebsite teamwebsite;
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            TeamWebsite.frm = new Form1();
new System.Threading.Thread(Program.initTray).Start();
Application.Run(TeamWebsite.frm);   
        }
public static void initTray()
{
    teamwebsite = new TeamWebsite();
Application.Run(teamwebsite);
}
 }
    class TeamWebsite : Form
    {
        public static Form1 frm;
        private NotifyIcon  trayIcon;
        private ContextMenu trayMenu;
        MenuItem[] m;

        public TeamWebsite()
        {
            trayMenu = new ContextMenu();
            m = new MenuItem[10];
            loggedOut();
            trayIcon      = new NotifyIcon();
            trayIcon.Text = "Kiwi NotifApp";
            trayIcon.Icon = frm.Icon;//new Icon("notifications.ico", 40, 40);


            trayIcon.ContextMenu = trayMenu;
            trayIcon.Visible     = true;
            trayIcon.DoubleClick += AppNotifications;
        }
        public void loggedIn()
        {
            for (int i = 0; i < m.Length; i++)
            {
                if (m[i] != null)
                trayMenu.MenuItems.Remove(m[i]);
            }
            for (int i = 0; i < m.Length; i++)
            {
                m[i] = null;
            }
            m[0] = new MenuItem("Notifications", AppNotifications);
            m[1] = new MenuItem("Logout",AppLogout);
            m[2] = new MenuItem("Exit", OnExit);
            for (int i = 0; i < m.Length; i++)
            {
                if (m[i] != null)
                trayMenu.MenuItems.Add(m[i]);
            }
        }
        public void loggedOut()
        {
            for (int i = 0; i < m.Length; i++)
            {
                if (m[i] != null)
                    trayMenu.MenuItems.Remove(m[i]);
            }
            for (int i = 0; i < m.Length; i++)
            {
                m[i] = null;
            }
           // m[0] = new MenuItem("Login", AppLogin);
m[0] = new MenuItem("Notifications", AppNotifications);
            m[1] = new MenuItem("Exit", OnExit);
            for (int i = 0; i < m.Length; i++)
            {
                if (m[i] != null)
                    trayMenu.MenuItems.Add(m[i]);
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            Visible       = false;
            ShowInTaskbar = false;

            base.OnLoad(e);
        }
        private void AppNotifications(object sender, EventArgs e)
        {
            if (frm.InvokeRequired)
            {
                frm.AppNotifications();
            }
        }
        private void AppLogin(object sender, EventArgs e)
        {
            if (frm.InvokeRequired)
            {
                frm.AppLogin();
            }
        }
        private void AppLogout(object sender, EventArgs e)
        {
            if (frm.InvokeRequired)
            {
                frm.AppLogout();
            }
        }
        

        private void OnExit(object sender, EventArgs e)
        {
            Application.Exit();
        }
	

        protected override void Dispose(bool isDisposing)
        {
            if (isDisposing)
            {
                trayIcon.Dispose();
            }

            base.Dispose(isDisposing);
        }
    }
}
